package com.huawei.hms.framework.network.restclient.hwhttp.dns;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.hianalytics.HianalyticsData;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.cache.CacheManager;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver.DNResolverManager;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver.dnkeeper.DNKeeper;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.log.DnsEventListener;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.log.DnsEventListener.Factory;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.network.NetworkStateReceiver;
import com.huawei.hms.framework.network.restclient.hwhttp.route.HostRoute;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DNManager {
    static final String TAG = "DNManager";
    private static volatile DNManager instance;
    private static volatile boolean isInit = false;
    private Context context;
    private DNKeeper dnKeeper;
    private ConcurrentHashMap<String, Integer> dnsCache = new ConcurrentHashMap();
    private ConcurrentHashMap<String, DnsFail> domainDns = new ConcurrentHashMap();
    private ConcurrentHashMap<String, DnsInfo> domainState = new ConcurrentHashMap();
    Factory eventListenerFactory;
    private ExecutorService executorService = Executors.newSingleThreadExecutor();

    public static class DnsFail {
        private int code = 0;
        private String ip;

        public String getIp() {
            return this.ip;
        }

        public void setIp(String str) {
            this.ip = str;
        }

        public int getCode() {
            return this.code;
        }

        public void setCode(int i) {
            this.code = i;
        }
    }

    private DNManager() {
    }

    public static DNManager getInstance() {
        if (instance == null) {
            synchronized (DNManager.class) {
                if (instance == null) {
                    instance = new DNManager();
                }
            }
        }
        return instance;
    }

    public static void dnsPrefetch(String str) {
        if (TextUtils.isEmpty(str)) {
            Logger.w(TAG, "dnsPrefetch, domain is empty");
        } else {
            DNResolverManager.dnsLazyUpdate(str, DNManager$ResolveTriggerType.DNS_PREFETCH);
        }
    }

    public static void dnsPrefetch(String[] strArr) {
        if (strArr == null || strArr.length == 0) {
            Logger.e(TAG, "dnsPrefetch: invalid parameter");
            return;
        }
        for (String dnsPrefetch : strArr) {
            dnsPrefetch(dnsPrefetch);
        }
    }

    public boolean isEnableDnsCache() {
        if (this.context != null && DnsConfig.isSupportDnsCache) {
            return true;
        }
        return false;
    }

    public void disableDnsCache() {
        DnsConfig.isSupportDnsCache = false;
    }

    public void setDnsTtl(int i) {
        int i2 = i * 1000;
        if (i2 < 60000 || i2 >= DnsConfig.MAX_DNS_RESULT_TTL) {
            i2 = 600000;
            Logger.w(TAG, "the ttl parameter invalid, set to default:" + 600000);
        }
        DnsConfig.defaultCacheTtl = (long) i2;
    }

    public void setRequestIntervalFailed(int i) {
        if (this.dnKeeper != null) {
            this.dnKeeper.setRequestIntervalFailed(i);
        }
    }

    public void init(final Context context, DNKeeper dNKeeper) {
        if (context == null) {
            Logger.e(TAG, "invalid parameter");
            return;
        }
        this.context = context.getApplicationContext();
        if (dNKeeper != null) {
            this.dnKeeper = dNKeeper;
        }
        if (!isInit) {
            synchronized (DNManager.class) {
                if (!isInit) {
                    isInit = true;
                    this.executorService.execute(new Runnable() {
                        public void run() {
                            NetworkStateReceiver.registerNetworkState(context.getApplicationContext());
                            if (DnsConfig.fileCacheStrategy == 0) {
                                CacheManager.loadFileCacheToMemeory();
                            }
                            String[] lastLaunchUsedDomains = HostRoute.getInstance().getLastLaunchUsedDomains();
                            if (lastLaunchUsedDomains != null) {
                                for (String str : lastLaunchUsedDomains) {
                                    Logger.v(DNManager.TAG, "init dnsLazyUpdate domain: " + str);
                                    if (DNManager.getInstance().getResolverSource(str) != 5) {
                                        DNResolverManager.dnsLazyUpdate(str, DNManager$ResolveTriggerType.DNS_INIT);
                                    }
                                }
                            }
                        }
                    });
                }
            }
        }
    }

    public DNKeeper getDNKeeper() {
        return this.dnKeeper;
    }

    public Context getContext() {
        return this.context;
    }

    @ResolverSource
    public int getResolverSource(String str) {
        DnsInfo resolverDnsInfo = getResolverDnsInfo(str);
        if (resolverDnsInfo == null) {
            return 4;
        }
        return resolverDnsInfo.getState();
    }

    public DnsInfo getResolverDnsInfo(String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        DnsInfo dnsInfo = (DnsInfo) this.domainState.get(str);
        if (dnsInfo != null && (this.dnKeeper != null || dnsInfo.getState() != 5)) {
            return dnsInfo;
        }
        if (dnsInfo == null) {
            dnsInfo = new DnsInfo();
        }
        dnsInfo.setState(4);
        this.domainState.put(str, dnsInfo);
        if (this.domainState.size() <= DnsConfig.MAX_CACHE_ENTRIES) {
            return dnsInfo;
        }
        Logger.w(TAG, "domain state hashmap exceed max size!");
        return dnsInfo;
    }

    public String getResolverAlph(@ResolverSource int i) {
        String str = HianalyticsData.DNS_TYPE_LOCAL;
        switch (i) {
            case 4:
                return HianalyticsData.DNS_TYPE_LOCAL;
            case 5:
                return HianalyticsData.DNS_TYPE_DNKEEPER;
            default:
                return str;
        }
    }

    public String getResolverAlph(String str) {
        return getResolverAlph(getResolverSource(str));
    }

    public void setResolverSource(String str, DnsInfo dnsInfo) {
        if (!TextUtils.isEmpty(str)) {
            this.domainState.put(str, dnsInfo);
        }
    }

    public void setResolverSource(String str, int i) {
        if (!TextUtils.isEmpty(str)) {
            DnsInfo dnsInfo = (DnsInfo) this.domainState.get(str);
            if (dnsInfo == null) {
                dnsInfo = new DnsInfo();
            }
            dnsInfo.setState(i);
            this.domainState.put(str, dnsInfo);
        }
    }

    public Factory getEventListenerFactory() {
        if (this.eventListenerFactory == null) {
            this.eventListenerFactory = DnsEventListener.factory(DnsEventListener.NONE);
        }
        return this.eventListenerFactory;
    }

    public void setEventListenerFactory(Factory factory) {
        if (factory != null) {
            this.eventListenerFactory = factory;
        }
    }

    public void addDns(String str, DnsFail dnsFail) {
        if (!TextUtils.isEmpty(str)) {
            this.domainDns.put(str, dnsFail);
        }
    }

    public DnsFail getDns(String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        return (DnsFail) this.domainDns.get(str);
    }

    public void deleteDns(String str) {
        if (!TextUtils.isEmpty(str)) {
            this.domainDns.remove(str);
        }
    }

    public void setDnsCache(String str, int i) {
        if (!TextUtils.isEmpty(str)) {
            this.dnsCache.put(str, Integer.valueOf(i));
        }
    }

    public int getDnsCache(String str) {
        if (TextUtils.isEmpty(str)) {
            return -1;
        }
        Integer num = (Integer) this.dnsCache.get(str);
        if (num == null) {
            num = Integer.valueOf(-1);
        }
        return num.intValue();
    }
}
